<template>
    <div >
        <!--搜索表单-->
        <div style="margin-bottom: 20px">
            <el-input style="width:240px" placeholder="请输入名称" v-model="params.name"></el-input>
            <el-input style="width:240px ;margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
            <el-button style="margin-left: 5px " type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
            <el-button style="margin-left: 5px " type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>

        </div>

        <el-table :data="tableData" stripe>
            <el-table-column prop="username" label="会员卡号"></el-table-column>
            <el-table-column prop="id" label="id"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="age" label="年龄"></el-table-column>
            <el-table-column prop="address" label="地址"></el-table-column>
            <el-table-column prop="phone" label="联系方式"></el-table-column>
            <el-table-column prop="sex" label="性别"></el-table-column>
            <el-table-column prop="createtime" label="创建时间"></el-table-column>
            <el-table-column prop="updatetime" label="更新时间"></el-table-column>

            <el-table-column label="操作">
                <template v-slot="scope">
                    <!--scope就是当前行的数据-->     <!--点击跳转到这个路由，带上ID-->
                <el-button type="primary"  @click="$router.push('/EditUser?id=' +scope.row.id)">编辑</el-button>
                    <el-popconfirm
                            style="margin-left: 5px"
                            title="确认删除这行数据吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button  type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <div style="margin-top: 10px">
            <el-pagination
                    background
                    :page-size="params.pageSize"
                    :current-page="params.pageNum"
                    layout="prev, pager, next"
                    @current-change="handleCurrentChange"
                    :total="total">

            </el-pagination>
        </div>
    </div>
</template>

<script>
    import request from "@/util/request";

    export default {
        name: 'User',
        data(){
            return{
                tableData:[],
                total: 0,
                //设置分页查询url携带的参数： http://localhost:9090/user/page?pageNum=1&pageSize=10&name=“”&phone=“”
                params:{
                    pageNum:1,
                    pageSize:10,
                    name:"",
                    phone:""
                },

            }
        },
        created() {
            this.load()
        },
        methods:{
            load(){
                // fetch("http://localhost:9090/user/page").then(res=>res.json()).then(res=>{
                //     console.log()
                //     this.tableData = res
                // })
                //使用封装好的request.js
                request.get('/user/page',{
                    params:this.params//拼接到url当中
                }).then(res => {
                    if(res.code =="200"){
                        this.tableData=res.data.list
                        this.total=res.data.total
                    }
                })
            },
            reset(){
                //重置按钮：直接把数据设置成默认
                this.params={
                    pageNum:1,
                    pageSize:10,
                    name:"",
                    phone:""
                }
                //再次调用查询所有
                this.load();
            },
            handleCurrentChange(pageNum){
                //点击分页按钮触发
                this.params.pageNum=pageNum;
                this.load();
            },
            del(id){
                request.delete('/user/delete/'+id).then(res=>{
                    if(res.code=="200"){
                        this.$notify.success("删除成功")
                        this.load()
                    }else{
                        this.$notify.error(res.msg)
                    }
                })
            }
        }

    }
</script>

<style scoped>

</style>
